const { ccclass, property } = cc._decorator;

@ccclass
export default class BannerAd extends cc.Component {
    private static _instance: any;
    public static get Instance(): BannerAd {
        if (BannerAd._instance == null) BannerAd._instance = new BannerAd();
        return BannerAd._instance;
    }

    static TestMode: boolean = false;
    static IsNative: boolean = false;
    static IsShouCang: boolean = false;

    private _appId: string = "105660676";
    private _videoId: string = "ad563c3fb6ad43aeaac4736733d67221";
    private _customId: string = "7d105dd5416e4138a9fa539223360a6d";
    private _bannerId: string = "431a18b034794afb82c3f9d1256fb549";

    private _year: number = 2024;
    private _month: number = 4;
    private _date: number = 30;
    private _hour: number = 19;

    public static cityIswhite: boolean = false;//是否为白名单

    static get Is_ANDROID() { return cc.sys.platform === cc.sys.ANDROID; }
    static get Is_VIVO_GAME() { return cc.sys.platform === cc.sys.VIVO_GAME; }
    static get Is_OPPO_GAME() { return cc.sys.platform === cc.sys.OPPO_GAME; }
    static get Is_HUAWEI_GAME() { return cc.sys.platform === cc.sys.HUAWEI_GAME; }

    isLogin: boolean = false;

    nativeAd = null;            //原生广告
    bannerAd = null;            //Banner广告
    rewardedVideoAd = null;     //激励视频
    customAd = null;            //原生模板

    ShowCustomAd(parent: Node = null) {
        console.log("弹出原生");
        if (BannerAd.TestMode) return;

        if (BannerAd.Is_OPPO_GAME) {
            if (BannerAd.IsNative) {
                this.CreateNative(parent);
            } else {
                this.CreateOVCustom();
            }
        }
        else if (BannerAd.Is_VIVO_GAME) {
            if (BannerAd.IsNative) {
                this.CreateNative(parent);
            } else {
                this.CreateOVCustom();
            }
        }
        else if (BannerAd.Is_HUAWEI_GAME) {
            if (BannerAd.IsNative) {
                this.CreateHWNative(parent);
            } else {
                this.CreateHWCustom();
            }
        }
        else if (BannerAd.Is_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "ChaPing", "()V");
        }
        // if (BannerAd.Instance.cityIswhite) this.ShowBanner();
    }

    ShowBanner() {
        console.log("弹出Banner");
        if (BannerAd.TestMode) return;

        if (BannerAd.Is_OPPO_GAME) {
            this.ShowOVBanner();
        }
        else if (BannerAd.Is_VIVO_GAME) {
            this.ShowOVBanner();
        }
        else if (BannerAd.Is_HUAWEI_GAME) {
            this.ShowHWBanner();
        }
        else if (BannerAd.Is_ANDROID) {
        }
    }

    ShowVideo(callback, args?: any) {
        console.log("弹出视频");
        if (BannerAd.TestMode) {
            if (args) {
                callback(args)
            } else {
                callback();
            }
            return;
        }
        if (!callback && !BannerAd.cityIswhite) return;
        if (BannerAd.Is_OPPO_GAME) {
            this.CreateOVVideo(callback, args);
        }
        else if (BannerAd.Is_VIVO_GAME) {
            this.CreateOVVideo(callback, args);
        }
        else if (BannerAd.Is_HUAWEI_GAME) {
            this.CreateHWVideo(callback, args);
        }
        else if (BannerAd.Is_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "GameVideo1", "()V");
            //@ts-ignore
            cc.playVideoSuccess1 = function () { callback && callback(args); }
        }
        else callback && callback(args);
    }

    private DestroyNativeAd() {
        if (BannerAd.IsNative) {
            this.DestroyNative();
        } else {
            this.DestroyCustom();
        }
    }

    private ShowOVBanner() {
        if (!this.TimeManager(this._year, this._month, this._date, this._hour, 0)) return;
        try {
            this.DestroyBanner();

            //@ts-ignore
            this.bannerAd = qg.createBannerAd({
                posId: BannerAd.Instance._bannerId,
                style: {
                    //vivo
                    // left: view.getVisibleSize().width / 2 - 150,
                    // top: view.getVisibleSize().height + 150
                },
                adIntervals: 30
            });

            //加载监听事件
            this.bannerAd.onLoad(this.onLoadCallBack_Banner);
            // this.bannerAd.onClose(this.onCloseCallBack_Banner);
            this.bannerAd.onError((e) => {
                console.log('load bannerAd error:' + JSON.stringify(e));
                console.log('bannerAd广告数据拉取失败:' + e.errMsg, e.errCode);
            });

            this.bannerAd.show();

        } catch (error) {
            console.log("Banner展示失败：" + error.message);
        }
    }

    private ShowHWBanner() {
        try {
            console.log(`加载 HWBanner ...`);
            this.DestroyBanner();

            //@ts-ignore
            var height = qg.getSystemInfoSync().safeArea.height;
            //@ts-ignore
            this.bannerAd = qg.createBannerAd({
                adUnitId: this._bannerId,
                style: {
                    top: height - 57,
                    left: 0,
                    height: 57,
                    width: 360,
                }
            });

            //加载监听事件
            this.bannerAd.onLoad(this.onLoadCallBack_Banner);
            this.bannerAd.onClose(this.onCloseCallBack_Banner);
            this.bannerAd.onError((e) => {
                const errCode = e.errCode
                const errMsg = e.errMsg
                console.error(`HWBanner 广告数据拉取失败:[${JSON.stringify(e)}]-[${errMsg}]-[${errCode}]`);
            });

            console.log(`加载 HWBanner 成功`);

            this.bannerAd.show();

        } catch (error) {
            console.error("Banner展示失败：" + error.message);
        }
    }

    private DestroyBanner() {
        try {
            //如果已经有了对象则先销毁对象
            if (!(typeof this.bannerAd == "undefined" || this.bannerAd == null)) {
                this.bannerAd.hide();
                this.bannerAd.offLoad(this.onLoadCallBack_Banner);
                this.bannerAd.offClose(this.onCloseCallBack_Banner);
                // this.bannerAd.offError(this.onErrorCallback_Banner);
                this.bannerAd.destroy();
                this.bannerAd = null;
            }
        } catch (error) {
            console.log(error);
        }
    }

    private HideBanner() {
        try {
            if (!(typeof this.bannerAd == "undefined" || this.bannerAd == null)) {
                this.bannerAd.hide();
                this.DestroyBanner();
            }
        } catch (error) {
            console.log(error);
        }
    }

    private onLoadCallBack_Banner() {
        try {
            this.bannerAd.offLoad(this.onLoadCallBack_Banner);
        } catch (error) {
            console.log(error);
        }
    }

    private onCloseCallBack_Banner() {
        try {
            this.bannerAd.offClose(this.onCloseCallBack_Banner);

            this.DestroyBanner();

        } catch (error) {
            console.log(error);
        }
    }

    private onErrorCallback_Banner() {
        try {
            //注销监听
            console.log('加载失败');
            // this.bannerAd.offError(this.onErrorCallback_Banner);
            this.DestroyBanner();
            this.ShowBanner();
        } catch (error) {
            console.log(error);
        }
    }

    private CreateOVVideo(callback, args?: any) {
        this.DestroyVideoAd();
        try {
            if (this.rewardedVideoAd == null) {
                //@ts-ignore
                this.rewardedVideoAd = qg.createRewardedVideoAd({
                    posId: BannerAd.Instance._videoId,
                });

                this.rewardedVideoAd.load();
                this.rewardedVideoAd.onLoad((data) => {
                    this.rewardedVideoAd.show();
                    console.info('激励视频数据拉取成功: ', data);
                })
                this.rewardedVideoAd.onError((e) => {
                    // UIManager.ShowTip("视频加载失败");
                    console.error('load ad error:' + JSON.stringify(e));
                    console.log('load ad error:' + e.errMsg, e.errCode);

                })
                this.rewardedVideoAd.onClose((res) => {
                    console.log('视频广告关闭回调')
                    if (res && res.isEnded) {
                        if (args) {
                            callback(args)
                        } else {
                            callback();
                        }
                        console.log("正常播放结束，可以下发游戏奖励");

                    } else {
                        console.log("播放中途退出，不下发游戏奖励");
                    }
                });
            } else {
                this.rewardedVideoAd.load();
            }
        } catch (error) {
            // UIManager.ShowTip("视频加载失败");
            console.error("激励视频异常信息：" + error.message);
        }
    }

    private CreateHWVideo(callback, args?: any) {
        this.DestroyVideoAd();
        try {
            if (this.rewardedVideoAd == null) {
                //@ts-ignore
                this.rewardedVideoAd = qg.createRewardedVideoAd({
                    adUnitId: this._videoId,
                });

                this.rewardedVideoAd.load();
                this.rewardedVideoAd.onLoad((data) => {
                    this.rewardedVideoAd.show();
                    console.info('激励视频数据拉取成功: ', data);
                })
                this.rewardedVideoAd.onError((e) => {
                    // UIManager.ShowTip("视频加载失败");
                    console.error('load ad error:' + JSON.stringify(e));
                    console.log('load ad error:' + e.errMsg, e.errCode);
                    this.DestroyVideoAd();
                    try {
                        cc.resources.load("videoTip", cc.Prefab, (e, n) => {
                            if (e) {
                                console.error(e);
                                return;
                            }
                            var node = cc.instantiate(n);
                            node.setParent(cc.find("GRoot"));
                            node.setPosition(cc.Vec2.ZERO);
                            setTimeout(() => {
                                node.destroy();
                            }, 2000);
                        })
                    } catch (error) {
                        console.log(error);
                    }

                })
                this.rewardedVideoAd.onClose((res) => {
                    console.log('视频广告关闭回调')
                    if (res && res.isEnded) {
                        if (args) {
                            callback(args)
                        } else {
                            callback();
                        }
                        console.log("正常播放结束，可以下发游戏奖励");

                    } else {
                        console.log("播放中途退出，不下发游戏奖励");
                    }
                });
            } else {
                this.rewardedVideoAd.load();
            }
        } catch (error) {
            // UIManager.ShowTip("视频加载失败");
            console.error("激励视频异常信息：" + error.message);
        }
    }

    private DestroyVideoAd() {
        try {
            //如果已经有了对象则先销毁对象
            console.log("rewardedVideoAd this.destroy", this.rewardedVideoAd);
            if (!(typeof this.rewardedVideoAd == "undefined") || this.rewardedVideoAd == null) {
                console.log("rewardedVideoAd this.destroy");
                this.rewardedVideoAd.offLoad();
                //this.rewardedVideoAd.offError();
                this.rewardedVideoAd.offClose();
                this.rewardedVideoAd.destroy();
                this.rewardedVideoAd = null;
            }
        }
        catch (error) {
            console.log("异常信息：" + error.message);
        }
    }

    private CreateOVCustom() {
        if (this.TimeManager(this._year, this._month, this._date, this._hour, 0)) {
            this.DestroyCustom();
            try {
                if (this.customAd == null) {
                    //@ts-ignore
                    this.customAd = qg.createCustomAd({
                        posId: BannerAd.Instance._customId,
                        style: {
                            // left: cc.view.getVisibleSize().width / 2 - 150,
                            // top: cc.view.getVisibleSize().height / 2 + 450
                        }
                    });

                    this.customAd.onError(err => {
                        console.log("原生模板广告加载失败", JSON.stringify(err));
                    });
                    this.customAd.show().then(() => {
                        console.log('原生模板广告展示完成');
                    }).catch((err) => {
                        console.log('原生模板广告展示失败', JSON.stringify(err));
                    })

                }
            } catch (error) {
                console.log(error);
            }
        }
    }

    private CreateHWCustom() {
        if (this.TimeManager(this._year, this._month, this._date, this._hour, 0)) {
            this.DestroyCustom();
            try {
                if (this.customAd == null) {
                    //@ts-ignore
                    this.customAd = qg.createCustomAd({
                        posId: this.nativeAd
                    });

                    this.customAd.onError(err => {
                        console.log("原生模板广告加载失败", JSON.stringify(err));
                    });
                    this.customAd.show().then(() => {
                        console.log('原生模板广告展示完成');
                    }).catch((err) => {
                        console.log('原生模板广告展示失败', JSON.stringify(err));
                    })

                }
            } catch (error) {
                console.log(error);
            }
        }
    }

    private DestroyCustom() {
        try {
            if (this.customAd != null) {
                this.customAd.offLoad();
                this.customAd = null;
            }
        }
        catch (error) {
            console.log("异常信息：" + error.message);
        }
    }

    dUnitVideoUrlList = null;       //广告视频
    adUnitImgUrl = null;            //广告图片
    adUnitAdid = null;              //广告标识，用于上报曝光与点击
    adUnitCreativeType = null;      //获取广告类型，取值说明如下：
    adUnitInteractionType = null;   //获取广告点击之后的交互类型
    source = null;                  //广告来源
    title = null;                   //广告标题
    logoUrl = null;                 //广告标签图片
    clickBtnTxt = null;             //点击按钮文本描述
    yuansheng_Node = null;

    private CreateNative(parent: Node = null) {
        if (this.TimeManager(this._year, this._month, this._date, this._hour, 0)) {
            try {
                this.DestroyNative();

                //@ts-ignore
                this.nativeAd = qg.createNativeAd({
                    posId: BannerAd.Instance._customId,
                })
                this.nativeAd.load();
                this.nativeAd.onLoad((data) => {
                    try {
                        console.info('ad data loaded: ' + JSON.stringify(data))
                        // var _data = eval(JSON.stringify(data));
                        // BannerAd.adUnitVideoUrlList = _data.adList[0]
                        this.adUnitImgUrl = data.adList[0].imgUrlList[0];
                        this.adUnitAdid = data.adList[0].adId;
                        this.adUnitCreativeType = data.adList[0].creativeType;
                        this.adUnitInteractionType = data.adList[0].interactionType;
                        this.source = data.adList[0].source;
                        this.title = data.adList[0].title;
                        this.logoUrl = data.adList[0].logoUrl;
                        this.clickBtnTxt = data.adList[0].clickBtnTxt;

                        console.info('广告数据拉取成功: ' +
                            '图片路径', this.adUnitImgUrl + '\n',
                            '广告标识，adid', this.adUnitAdid + '\n',
                            '广告类型', this.adUnitCreativeType + '\n',
                            '广告点击之后的交互类型', this.adUnitInteractionType + '\n',
                            '广告来源', this.source + '\n',
                            '广告标题', this.title + '\n',
                            '广告标签图片', this.logoUrl + '\n',
                            '点击按钮文本描述', this.clickBtnTxt);

                        cc.resources.load("Prefabs/UI/OppoCustomAd", cc.Prefab, function (err, res) {
                            if (err) {
                                console.log("预制体不存在");
                                return;
                            }
                            if (BannerAd.Instance.yuansheng_Node != null) {
                                BannerAd.Instance.yuansheng_Node.destroy();
                            }
                            BannerAd.Instance.yuansheng_Node = cc.instantiate(res);

                            if (parent) {
                                BannerAd.Instance.yuansheng_Node.parent = parent;
                            } else {
                                BannerAd.Instance.yuansheng_Node.parent = cc.find("Canvas");
                            }
                        });
                    } catch (error) {
                        console.log(error);
                    }
                });
                this.nativeAd.onError((e) => {
                    console.log('load ad error:' + JSON.stringify(e));
                    console.log('广告数据拉取失败:' + e.errMsg, e.errCode);
                });
            }
            catch (error) {
                console.log("异常信息：" + error.message);
            }
        }
    }

    private CreateHWNative(parent: Node = null) {
        if (!this.TimeManager(this._year, this._month, this._date, this._hour, this._hour)) return;
        try {
            this.DestroyNative();

            console.log(`加载 HW原生自渲染...`);

            //@ts-ignore
            this.nativeAd = qg.createNativeAd({
                adUnitId: this._customId,
                style: {
                    left: 0,
                    top: 0
                }
            })
            this.nativeAd.load();
            this.nativeAd.onLoad((data) => {
                // var _data = eval(JSON.stringify(data));
                // BannerAd.adUnitVideoUrlList = _data.adList[0]
                this.adUnitImgUrl = data.adList[0].imgUrlList[0];
                this.adUnitAdid = data.adList[0].adId;
                this.adUnitCreativeType = data.adList[0].creativeType;
                this.adUnitInteractionType = data.adList[0].interactionType;
                this.source = data.adList[0].source;
                this.title = data.adList[0].title;
                this.logoUrl = data.adList[0].logoUrl;
                this.clickBtnTxt = data.adList[0].clickBtnTxt;

                // this.NativeImage.spriteFrame.setTexture = this.adUnitImgUrl;  
                // this.NativeImage.node.active = true;
                console.info('广告数据拉取成功: ' +
                    '图片路径', this.adUnitImgUrl + '\n',
                    '广告标识，adid', this.adUnitAdid + '\n',
                    '广告类型', this.adUnitCreativeType + '\n',
                    '广告点击之后的交互类型', this.adUnitInteractionType + '\n',
                    '广告来源', this.source + '\n',
                    '广告标题', this.title + '\n',
                    '广告标签图片', this.logoUrl + '\n',
                    '点击按钮文本描述', this.clickBtnTxt);

                cc.resources.load("Prefabs/UI/OppoCustomAd", cc.Prefab, function (err, res) {
                    console.log(`加载 HW原生自渲染预制体...`);
                    if (err) {
                        console.error(`HWNative 预制体不存在此路径:"HW/OppoCustomAd [${err}]"`);
                        return;
                    }
                    if (BannerAd.Instance.yuansheng_Node != null) {
                        BannerAd.Instance.yuansheng_Node.destroy();
                    }
                    BannerAd.Instance.yuansheng_Node = cc.instantiate(res);
                    console.log(BannerAd.Instance.yuansheng_Node);
                    if (parent != null) {
                        BannerAd.Instance.yuansheng_Node.parent = parent;
                    } else {
                        BannerAd.Instance.yuansheng_Node.parent = cc.find("Canvas");
                    }
                    BannerAd.Instance.yuansheng_Node.setPosition(0, 0, 0);
                    console.log(`加载 HW原生自渲染预制体成功`);
                    // Banner.Instance.yuansheng_Node.setSiblingIndex(0);
                    // Banner.Instance.yuansheng_Node.zIndex = 500;

                });
            });

            this.nativeAd.onError((e) => {
                const errCode = e.errCode
                const errMsg = e.errMsg
                console.error(`HW 自渲染广告数据拉取失败:[${JSON.stringify(e)}]-[${errMsg}]-[${errCode}]`);
            });

            console.log(`加载 HW原生自渲染成功`);
        } catch (error) {
            console.error("HW原生自渲染失败：" + error.message);

        }
    }

    private DestroyNative() {
        try {
            if (this.nativeAd != null) {

                this.nativeAd.offLoad();
                this.nativeAd.destroy();
                console.log("原生销毁：");
                // this.nativeAd.offError();
            } else {

                // this.nativeAd.offError();
            }
        }
        catch (error) {
            console.log("异常信息：" + error.message);
        }
    }

    icon = null;
    /**创建桌面图标 */
    CreateIcon(success: Function = null, fail: Function = null) {
        //@ts-ignore
        qg.hasShortcutInstalled({
            success: function (status) {
                // 判断图标未存在时，创建图标
                if (status == false) {
                    //@ts-ignore
                    qg.installShortcut({
                        success: function () {
                            // 执行用户创建图标奖励
                            BannerAd.IsShouCang = true;
                            success && success();
                        },
                        fail: function (err) {
                            console.log(err)
                            fail && fail();
                        },
                        complete: function () {

                        }
                    })
                } else {
                }
            },
            fail: function (err) { },
        })
    }

    public boxPortalAd = null

    // 游戏首页等适合展示Icon的场景常驻展示（每次回到此页面均调用一次）
    private ShowBoxPortalAd() {
        //@ts-ignore
        if (qg.createBoxPortalAd) {
            //@ts-ignore
            this.boxPortalAd = qg.createBoxPortalAd({
                posId: "",
                image: '',
                marginTop: 200
            })
            this.boxPortalAd.onError(function (err) {
                console.log("盒子九宫格广告加载失败", err)
            })
            this.boxPortalAd.onClose(function () {
                console.log('close')
                if (this.boxPortalAd.isDestroyed) {
                    return
                }
                // 当九宫格关闭之后，再次展示Icon
                this.boxPortalAd.show()
            })
            // 广告数据加载成功后展示
            this.boxPortalAd.show();
            this.boxPortalAd.onShow(() => {
                console.log('show success')
            })
        } else {
            console.log('暂不支持互推盒子相关 API')
        }
    }

    // 场景切换等需要关闭时调用
    CloseBoxPortalAd() {
        if (this.boxPortalAd != null) {
            this.boxPortalAd.isDestroyed = true
            this.boxPortalAd.destroy()
        }
    }

    AndroidKeFu() {
        if (BannerAd.Is_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "KeFu", "()V");
        }
        console.log("点击联系客服")
    }

    AndriodPrivacy() {
        if (BannerAd.Is_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "Hide", "()V");
        }
        console.log("点击隐私政策")
    }

    AndroidLogout() {
        if (BannerAd.Is_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "cleanPackageData", "()V");
        }
        console.log("点击注销")
    }

    AndroidMoreGame() {
        if (BannerAd.Is_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "MoreGame", "()V");
        }
        console.log("点击更多精彩")
    }

    Quit() {
        if (BannerAd.Is_OPPO_GAME || BannerAd.Is_VIVO_GAME || BannerAd.Is_HUAWEI_GAME) {

            //@ts-ignore
            qg.exitApplication({
                success: function () {
                    console.log("exitApplication success");
                },
                fail: function () {
                    console.log("exitApplication fail");
                },
                complete: function () {
                    console.log("exitApplication complete");
                }
            });
        }

        else if (BannerAd.Is_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "QuitGame", "()V");
        }
    }

    HWGameLogin(successCallback, failCallback) {
        if (BannerAd.TestMode) {
            successCallback();
            return;
        }

        console.log(`HW游戏登陆...`);

        //@ts-ignore
        qg.gameLoginWithReal({
            forceLogin: 1,
            appid: this._appId,
            success: (data) => {
                console.log(`HW游戏登陆成功：${JSON.stringify(data)}`);
                successCallback();
            },
            fail: (data, code) => {
                console.error(`HW游戏登陆失败：${data} -data：${data} -code：${code}`);
                //状态码为7004或者2012，表示玩家取消登录。
                //此时，建议返回游戏界面，可以让玩家重新进行登录操作。
                if (code == 7004 || code == 2012) {
                    console.log("玩家取消登录，返回游戏界面让玩家重新登录。")
                    failCallback();
                }
                //状态码为7021表示玩家取消实名认证。
                //在中国大陆的情况下，此时需要禁止玩家进入游戏。
                if (code == 7021) {
                    console.log("The player has canceled identity verification. Forbid the player from entering the game.")
                    cc.game.end();
                }
            }
        });
    }

    TimeManager(_year, _month, _date, _h, _m): boolean {
        var nowdate = new Date();
        var year = nowdate.getFullYear();           //年
        var month = nowdate.getMonth() + 1;         //月 获取当前月（注意：返回数值为0~11，需要自己+1来显示）
        var date = nowdate.getDate();               //日
        var day = nowdate.getDay();                 //周几
        var h = nowdate.getHours()                  //小时
        var m = nowdate.getMinutes()                //分钟
        var s = nowdate.getSeconds()                //秒

        if (year > _year) {
            return true
        } else if (year == _year) {
            if (month > _month) {
                return true;
            } else if (month == _month) {
                if (date > _date) {
                    return true;
                } else if (date == _date) {
                    if (h > _h) {
                        return true;
                    } else if (h == _h) {
                        if (m >= _m) {
                            return true;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    cityIswhite = false;
    GetcityIswhite() {
        return this.cityIswhite = true;
        if (BannerAd.Is_VIVO_GAME) {
            cc.assetManager.loadRemote("https://yxapi.tomatojoy.cn/getIp", (err, res) => {
                //@ts-ignore
                var province = JSON.parse(res._nativeAsset).data.province;//黑名单省份
                //@ts-ignore
                var city = JSON.parse(res._nativeAsset).data.city;//黑名单城市
                let IPAreas = ["石家庄", "太原市", "沈阳市", "长春市", "哈尔滨市", "南京市", "杭州市", "合肥市",
                    "福州市", "南昌市", "济南市", "郑州市", "长沙市", "武汉市", "海口市", "成都市", "贵阳市", "昆明市",
                    "西安市", "兰州市", "西宁市", "北京市", "天津市", "上海市", "重庆市", "大连市", "青岛市", "厦门市", "广东省"];
                for (let i in IPAreas) {
                    if (city == IPAreas[i]) {
                        this.cityIswhite = false;
                        return;
                    }
                }
                for (let i in IPAreas) {
                    if (province == IPAreas[i]) {
                        this.cityIswhite = false;
                        return;
                    }
                }
                this.cityIswhite = true;
            })
        }
        if (BannerAd.Is_OPPO_GAME || BannerAd.Is_ANDROID) {
            //地区IP判定
            let xhr = new XMLHttpRequest();
            xhr.open('get', 'https://yxapi.tomatojoy.cn/getIp', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status >= 200 && xhr.status < 400) {
                        let json = JSON.parse(xhr.responseText);
                        let data = json.data;
                        let IPAreas = ["石家庄", "太原市", "沈阳市", "长春市", "哈尔滨市", "南京市", "杭州市", "合肥市",
                            "福州市", "南昌市", "济南市", "郑州市", "长沙市", "武汉市", "海口市", "成都市", "贵阳市", "昆明市",
                            "西安市", "兰州市", "西宁市", "北京市", "天津市", "上海市", "重庆市", "大连市", "青岛市", "厦门市", "广东省"];
                        for (let i in IPAreas) {
                            if (data.city == IPAreas[i]) {
                                this.cityIswhite = false;
                                return;
                            }
                        }
                        for (let i in IPAreas) {
                            if (data.province == IPAreas[i]) {
                                this.cityIswhite = false;
                                return;
                            }
                        }
                    }
                    else {
                        cc.log({ code: -1, msg: '请求失败' })
                    }
                }
                this.cityIswhite = true;
            }

        }
    }
}
//@ts-ignore
window.BannerAd = BannerAd;